package com.lab.service.impl;

import com.lab.dao.TeacherDao;
import com.lab.pojo.Teacher;
import com.lab.service.TeacherService;
import com.lab.util.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("teacherService")
public class TeacherServiceImp implements TeacherService {
    @Resource
    TeacherDao teacherDao;
    //查询所有数据
    public List<Teacher> allTeachers() {
        return teacherDao.allTeachers();
    }
    //添加和修改
    public void saveTeacher(Teacher teacher) {
        try {
            Teacher t= teacherDao.findTeacherById(teacher.getTid());
            if (t!=null){
                //修改
                teacherDao.updateTeacher(teacher);
            }else {
                //增加
                // Md5对密码加密
                teacher.setPassword(Md5Util.encodeByMd5(teacher.getPassword()));
                SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
                sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
                Date date = new Date();
                teacher.setCreate_time(sdf.format(date));
                teacherDao.addTeacher(teacher);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //删除
    public void deleteTeacher(Long[] tid) {
        teacherDao.deleteTeacher(tid);
    }

    public Teacher findTeacherById(Long tid) {
        return teacherDao.findTeacherById(tid);
    }

    @Override
    public Teacher findTeacher(Long tid) {
        return teacherDao.findTeacher(tid);
    }
}
